# $Id: grid.py 7557b5d4d82b 2012/12/02 09:07:16 pgurumur $
# Copyright (c) 2009 Prabhu Gurumurthy  <pgurumur@gmail.com>
# 
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 

__version__ = "$Revision: 7557b5d4d82b $"
__author__ = "$Author: pgurumur $"

import abc
from django.conf import settings
from django.db import models

from djanet.lib import stdlib
from djanet.lib.exception import GridError

class GridBase(object):
   __metaclass__ = abc.ABCMeta

   def __init__(self):
      self._count = 0

   @abc.abstractmethod
   def layout(self):
      raise NotImplementedError()

   @abc.abstractmethod
   def values(self):
      raise NotImplementedError()

   @property
   def my_model(self):
      if not hasattr(self, 'model'):
         raise GridError, "no model defined!"

      return self.model

   def __str__(self):
      return stdlib.my_name(self.model)

   def __repr__(self):
      return repr(str(self))

   def __unicode__(self):
      return unicode(str(self))

class GridField(object):
   def __init__(self, **kwargs):
      self._layout = {}
      self._name = None

      if "field" in kwargs:
         self._layout["field"] = kwargs["field"]
      
      if "name" in kwargs:
         self._layout["name"] = kwargs['name']
         self._name = kwargs['name']

      if "width" in kwargs:
         self._layout["width"] = kwargs["width"]

      if "editable" in kwargs:
         self._layout["editable"] = kwargs["editable"]

      if not "width" in kwargs:
         self._layout["width"] = 'auto'

      if not "editable" in kwargs:
         self._layout["editable"] = False

   @property
   def layout(self):
      return self._layout

   def __str__(self):
      return stdlib.my_str(self_name)

   def __repr__(self):
      return repr(str(self))

   def __unicode__(self):
      return unicode(str(self))

class ModelGrid(object):
   def __init__(self, App, Model):
      super(stdlib.my_class(self), self).__init__()
      self._err = GridError()
      self._app = None
      self._modelName = None
      self._model = None
      self._appList = []
      self._grid = None

      if App:
         self._app = App
      else:
         raise GridError, "No django app defined!"

      if Model:
         self._modelName = Model
      else:
         raise GridError, "No model defined!"

      self._model = models.get_model(self._app, self._modelName)
      if self._model is None:
         raise GridError, "Cannot find the app/model"

      if not self._model in models.get_models():
         raise GridError, "Invalid model, check the app/model"

      if not hasattr(settings, 'GRIDAPP'):
         raise GridError, "No gridapp field defined!"

      for ix in GridBase.__subclasses__():
         instance = ix()
         if self._model == instance.my_model:
            self._grid = instance

      if self._grid is None:
         raise GridError, "%s does not have grid defined!" %self._modelName

   def __str__(self):
      return stdlib.my_str(self._modelName)

   def __repr__(self):
      return repr(str(self))

   def __unicode__(self):
      return unicode(str(self))

   @property
   def model(self):
      return self._model

   @property
   def grid(self):
      return self._grid
